/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aptech.edu.ixj.managebean;

import aptech.edu.ixj.dto.AnswerDto;
import aptech.edu.ixj.dto.QuestionDto;
import aptech.edu.ixj.dto.SurveyDto;
import aptech.edu.ixj.impl.QALogicImpl;
import aptech.edu.ixj.impl.SurveyLogicImpl;
import aptech.edu.ixj.interfaces.IQALogic;
import aptech.edu.ixj.interfaces.ISurveyLogic;
import java.util.List;
import org.w3c.dom.events.Event;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author ngadtt
 */
@ManagedBean
@SessionScoped
public class ManageBean {

    private IQALogic qALogic = new QALogicImpl();
    private ISurveyLogic surveyLogic = new SurveyLogicImpl();
    QuestionDto question;
    List<SurveyDto> surveys;
    List<AnswerDto> answers;
    List<SurveyDto> lsSurvey;
    private String idQuestion;
    private String contentQuestion;

    public String getContentQuestion() {
        return contentQuestion;
    }

    public List<SurveyDto> getLsSurvey() {
        System.out.println(surveyLogic.getListSurvey().size()+"fddfdfdfdfdfdf");
        return surveyLogic.getListSurvey();
    }

    public void setLsSurvey(List<SurveyDto> lsSurvey) {
        this.lsSurvey = lsSurvey;
    }

    

    public void setContentQuestion(String contentQuestion) {
        this.contentQuestion = contentQuestion;
    }

    public String getIdQuestion() {
        return idQuestion;
    }

    public void setIdQuestion(String idQuestion) {
        this.idQuestion = idQuestion;
    }

    public String displaySurvey() {
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        String surveyId = "";
        if (request.getParameter("surveyId") != null) {
            surveyId = request.getParameter("surveyId");
            setSurveys(surveyLogic.readSurvey(surveyId));
        }
        return "index";
    }





    public String createQuestion() {
        question = new QuestionDto();
        question.setIdQuestion(idQuestion);
        question.setContent(contentQuestion);
        qALogic.createQuestion(question);

        return "createanswer";
    }

//    public static void main(String[] args) {
//
//    }
    public void initPage(Event PhaseEvent) {
    }

    public List<AnswerDto> getAnswers() {
        return answers;
    }

    public void setAnswers(List<AnswerDto> answers) {
        this.answers = answers;
    }

    public QuestionDto getQuestion() {
        return question;
    }

    public void setQuestion(QuestionDto question) {
        this.question = question;
    }

    public List<SurveyDto> getSurveys() {
        return surveys;
    }

    public void setSurveys(List<SurveyDto> surveys) {
        this.surveys = surveys;
    }

    public void done() {
    }

    public String display() {
        for (SurveyDto survey : surveys) {
            for (QuestionDto quest : survey.getQuestion()) {
                for (AnswerDto ans : quest.getAnswer()) {
                    if (ans.isChecked()) {
                        int rate = Integer.parseInt(ans.getRate()) + 1;
                        break;
                    }
                }
            }
        }
        return "finish";
    }

    public void submit() {
    }
}
